﻿//--------------------------------------------------------------------------------------------------------------------- 
// <copyright file="ApproveFiles.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights reserved.
// </copyright>
// <summary>
//   Defines the ApproveFiles type.
// </summary>
//---------------------------------------------------------------------------------------------------------------------
using Collaboris.Saf.Actions.Wss.Entities;

namespace Collaboris.Saf.Actions.Wss.File
{
    using System.Collections.Generic;

    using Engine;

    using Microsoft.SharePoint;

    using SharePoint.Wss.Attributes;
    using SharePoint.Wss.Files;

    using Utils;

    [CollaborisClass(
        Title = "Approve Files",
        ReleaseStatus = ReleaseStatus.Beta,
        DisableDocumentation = false,
        ContactAuthorEmail = "Support@Collaboris.co.uk",
        Summary = "Approves a list of files contained within the list.",
        Remarks = @"Please note. The undo currently performs no action.",
        ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C644}"
    )
    ]
    public class ApproveFiles : WSSActionBase<ApproveFileInfo>
    {
    
        #region Methods (6) 

        /// <summary>
        /// This method will ApproveFile a list of Files.
        /// </summary>
        /// <param name="context">
        /// </param>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <returns>
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, ApproveFileInfo entity)
        {
            this.Trace.TraceMethodStart("DoAction");

            ValidationHelper.VerifyObjectArgument(TargetWeb, "TargetWeb");
            ValidationHelper.VerifyObjectArgument(entity, "entity");

            ProcessStatus status = RunApproveFiles(entity, TargetWeb, ProcessMode.Do);

            this.Trace.TraceMethodEnd("DoAction");

            return status;
        }

        /// <summary>
        /// Checks the Data entity for Validity
        /// </summary>
        /// <param name="context">The <see cref="IContext"/>context.</param>
        /// <param name="entity">The data entity to Validate</param>
        /// <param name="errors">List of <see cref="ValidationError"/>s</param>
        /// <param name="mode"><see cref="ProcessMode"/> signalling wheter we are Doing, or Undoing the action.</param>
        /// <returns>false if invalid</returns>
        protected override bool IsValid(SafContext context, ApproveFileInfo entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;
            ValidationHelper.VerifyObjectArgument(entity.FileUrls, "entity.FileUrls");
            
            // Have we added errors ?
            return errors.Count == currentCount;
        }


        /// <summary>
        /// Checks out the specified the file as spec'd in <see cref="ListItemInfo"/>
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="web">The web.</param>
        /// <param name="mode">The mode.</param>
        /// <returns></returns>
        private ProcessStatus RunApproveFiles(ApproveFileInfo entity, SPWeb web, ProcessMode mode)
        {
            this.Trace.TraceMethodStart("RunApproveFiles");

            if (mode == ProcessMode.Do)
            {
                SPFileHelper.ApproveFiles(TargetWeb,entity.FileUrls,entity.ApproveComments);
            }

            this.Trace.TraceMethodEnd("RunCheckInFile");
            return ProcessStatus.Success;
        }
        #endregion Methods 
    }
}